app.controller("informPageCtrl",function($scope,$http,$ionicLoading){
    //用户数据
    var userInfo = JSON.parse(localStorage.getItem("UserInfo"));
    // var getPath = JSON.parse(localStorage.getItem("getPath"));
    //已阅/未阅
    $scope.isNoReadActive = true;
    $scope.isReadActive = false;
    $scope.changeList = function () {
        $scope.isNoReadActive = !$scope.isNoReadActive;
        $scope.isReadActive = !$scope.isReadActive;
        var isActive = 0;//是否阅读
        if($scope.isReadActive){
            isActive = 1
        }

        $scope.getNoticeList(isActive);
    }
    //标记已读
    $scope.read = function (obj) {
        if(obj.content=="暂无通知！"){
            return;
        }
        var url = getPath.url+getPath.name+"/app/updateNotice";
        var param = {
            id:obj.noticeid,
            userId:userInfo.userId
        }
        $http({
            url:url,
            method:"POST",
            data:param,
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded'
            },
        }).success(function(data){
                if(data.resultCode==200){
                    obj.isShow = !obj.isShow;
                    //缓存中的通知数-1
                    userInfo.nNoticeCnt = userInfo.nNoticeCnt-1;
                    localStorage.setItem("UserInfo",JSON.stringify(userInfo));
                }else{
                    $ionicLoading.show({
                        template: "服务器异常,请联系管理员!",
                        duration:3000
                    });
                }
            })
            .error(function(){
                $ionicLoading.show({
                    template: "服务器异常,请联系管理员!",
                    duration:3000
                });
            });
    }
    //获取消息列表绑定数据
    $scope.getNoticeList = function (isRead) {
        var url = getPath.url+getPath.name+"/app/getNoticeList";
        var param = {
            userId:userInfo.userId,
            hadread:isRead
        }
        $http({
            url:url,
            method:"POST",
            data:param,
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded'
            },
        }).success(function(data){
            var res = data.resultBean;
            if(data.resultCode==200){
                if(res){
                    var _len = res.length;
                    for(var i=0;i<_len;i++){
                        res[i].isShow = true;
                        if(isRead==1){
                            res[i].showBtn = false;
                        }else{
                            res[i].showBtn = true;
                        }
                    }
                }else{
                    res = [{name:"暂无通知！",content:"暂无通知！"}];
                }

                $scope.notices = res;
            }else{
                $ionicLoading.show({
                    template: "服务器异常,请联系管理员!",
                    duration:3000
                });
            }
        })
        .error(function(data){
            $ionicLoading.show({
                template: data.result,
                duration:3000
            });
        });
    }
    $scope.getNoticeList();
});